---
category: Misc
created: '2020-05-31'
title: Redirect the page to HTTPS if it is in HTTP
updated: '2021-10-13'
---

**JavaScript version**

```js redirectHttps.js
const redirectHttps = () =>
    location.protocol === 'https:' ? {} : location.replace(`https://${location.href.split('//')[1]}`);

// Or
const redirectHttps = () => (location.protocol === 'https:' ? {} : (location.protocol = 'https:'));
```

**TypeScript version**

```ts redirectHttps.ts
const redirectHttps = (): void =>
    location.protocol === 'https:' ? void 0 : location.replace(`https://${location.href.split('//')[1]}`);

// Or
const redirectHttps = (): string => (location.protocol === 'https:' ? '' : (location.protocol = 'https:'));
```
